const service = require('./auctionService.js')

Page({
  data: {
    tabs: ["竞拍中", "已送出"],
    anctionData: null,
    sendData: null,
    anctions: [],
    sends: [],
    isLoading: true,
  },

  onLoad: function () {
    this.anctionPage = 0
    this.sendPage = 0

    wx.showLoading({
      title: '加载中',
    });
    this.requestAnction()
    this.requestSend()
  },

  configArray: function(value) {
    for (var index in value) {
      value[index].saleTypeId = getApp().constant.BIDDERS
      value[index].id = value[index].goodsId
      value[index].saleText = "我出价"
      value[index].store = value[index].isStore
    }
    return value
  },

  requestAnction: function () {
    var that = this;

    service.requestProducts(this.anctionPage, 0, function (result) {
      wx.hideLoading()
      var array = that.data.anctions.concat(that.configArray(result.items))
      that.setData({
        anctionData: result,
        anctions: array,
        isLoading:false
      })
    });
  },

  requestSend: function () {
    var that = this;

    service.requestProducts(this.sendPage, 1, function (result) {
      wx.hideLoading()

      var array = that.data.sends.concat(that.configArray(result.items))
      that.setData({ 
        sendData: result,
        sends: array,
        isLoading:false
      })
    });
  },
  upper: function (e) {
    if (e.detail == 0) {
      this.anctionPage = 0
      this.data.anctions.splice(0, this.data.anctions.length)
      this.requestAnction()
    } else {
      this.sendPage = 0
      this.data.sends.splice(0, this.data.sends.length)
      this.requestSend()
    }
  },

  lower(e) {
    if (e.detail == 0) {
      if (this.data.anctionData.hasNextPage == true) {
        this.anctionPage += 1
        this.requestAnction()
      }
    } else {
      if (this.data.sendData.hasNextPage == true) {
        this.sendPage += 1
        this.requestSend()
      }
    }
  }

});